package app.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.media.bean.playerbean.MediaPlayer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import app.action.PlayerAction;
import app.action.QuestionUI_ControlAction;
import app.action.TickAnswerAction;
import app.textSrc.TextSource;

public class QuestionUI extends AbsCommonPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4148977951583018172L;

	private MediaPlayer mediaPlayer;
	private TextSource textSource;
	private JTextArea jTextArea; // for Tape script display
	private JScrollPane jScrollPane; // for Tape script display
	private JLabel[] jLabels; // 3 Labels
	private JCheckBox[][] jCheckBoxes; // 3 x 4 check boxes
	private final String[] btnLables = { "Play", "Stop", "Answer", "Home",
			"Back", "Next" };
	private final String[] btnLables2 = { "Pause", "Reset" };
	private JButton[] jButtons;
	private JButton btnShowTS;
	private int index = 0; // handle the index for count questions index, count
							// from 0
	private int maxIndex = 0;
	private boolean autoAddHead = false; // disable auto add index in questions,
											// and in answers
	private boolean showAnsewr = false;
	private QuestionUI_ControlAction controlAction;
	TickAnswerAction tickAnswerAction;

	public QuestionUI(MainFrame mFrame, boolean autoAddHead) {
		super(mFrame);
		this.textSource = mFrame.getTextSource();
		this.autoAddHead = autoAddHead;
		this.maxIndex = getTextSource().getAmtQue() / 3;

		if (maxIndex > 0) {
			doAddComponents();
		}
	}

	@Override
	protected void doAddComponents() {
		addQuestions();

		// add components displaying tape script
		jTextArea = new JTextArea();
		jScrollPane = new JScrollPane(jTextArea);
		add(jScrollPane);

		// add media player
		mediaPlayer = new MediaPlayer();
		add(mediaPlayer);

		initializeComponents();
	}

	@Override
	public void initializeComponents() {
		initializeQuestion();
		initializeControl();

		// add key listener
		Component[] components = this.getComponents();
		for (Component comp : components) {
			comp.addKeyListener(controlAction);
		}

		// init components displaying tape script
		Font font = mFrame.getAppInit().getFont2();
		jTextArea.setWrapStyleWord(true);
		jTextArea.setFont(font);
		jScrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		showTapeScript(false);

		// init media player
		mediaPlayer.setVisible(true);
	}

	public void resizeComponents() {
		final int num = btnLables.length;
		final int btnHei = 30;
		final int btnWid = this.getWidth() / (num + 1);

		int x = 34;
		int y = 5;
		int width = this.getWidth() - x * 2;
		int height = this.getHeight() - 2 * btnHei - y;

		jScrollPane.setBounds(x, y, width, height);
		jScrollPane.updateUI();

		height /= 15;
		for (int i = 0; i < 3; i++) {
			jLabels[i].setBounds(x, y, width, height);
			y += height;
			for (int j = 0; j < 4; j++) {
				jCheckBoxes[i][j].setBounds(x, y, width, height);
				y += height;
			}
		}
		// media player
		x = btnWid / 2;
		// y = this.getHeight() - 2 * btnHei;
		width = this.getWidth() - btnWid;
		height = btnHei - 2;
		mediaPlayer.setBounds(x, y, width, height);

		// btn show tape script
		x = 0;
		y = this.getHeight() - btnHei;
		width = btnWid;
		height = btnHei;
		btnShowTS.setBounds(x, y, width, height);

		x += btnWid;
		for (JButton jButton : jButtons) {
			jButton.setBounds(x, y, btnWid, btnHei);
			x += btnWid;
		}
	}

	private void addQuestions() {
		jLabels = new JLabel[3];
		jCheckBoxes = new JCheckBox[3][4];
		this.tickAnswerAction = new TickAnswerAction(this);

		for (int i = 0; i < 3; i++) {
			jLabels[i] = new JLabel();
			add(jLabels[i]);

			for (int j = 0; j < 4; j++) {
				jCheckBoxes[i][j] = new JCheckBox();
				jCheckBoxes[i][j].addMouseListener(tickAnswerAction);
				add(jCheckBoxes[i][j]);
			}
		}
	}

	public void initializeQuestion() {
		String[] ques = new String[3];
		String[][] anss = new String[3][4];
		int questionIndex = index * 3;
		int num = 3;

		if (index == maxIndex - 1) {
			if (getTextSource().getAmtQue() % 3 != 0) {
				num = getTextSource().getAmtQue() % 3;
			}
		}

		for (int i = 0; i < num; i++) {
			if (autoAddHead) {
				ques[i] = "Question " + (i + 1) + ": "
						+ textSource.getQueStr()[questionIndex + i];
			} else {
				ques[i] = textSource.getQueStr()[questionIndex + i];
			}

			jLabels[i].setText(ques[i]);
			setJLabelProperties(jLabels[i]);

			for (int j = 0; j < 4; j++) {
				if (autoAddHead) {
					anss[i][j] = TextSource.ansLetStr.charAt(j) + " ."
							+ textSource.getAnsStr()[questionIndex + i][j];
				} else {
					anss[i][j] = textSource.getAnsStr()[questionIndex + i][j];
				}

				jCheckBoxes[i][j].setText(anss[i][j]);
				setJCheckBoxProperties(jCheckBoxes[i][j], i, j);
			}
		}
	}

	private void setJLabelProperties(JLabel jLabel) {
		Font f = mFrame.getAppInit().getFont1();
		jLabel.setFont(f);
		jLabel.setOpaque(true);
	}

	private void setJCheckBoxProperties(JCheckBox jCheckBox, int i, int j) {
		Font f = mFrame.getAppInit().getFont2();
		jCheckBox.setFont(f);
		jCheckBox.setBackground(mFrame.getAppInit().getCol1());
		jCheckBox.setForeground(Color.BLACK);
		jCheckBox.setSelected(false);
	}

	private void initializeControl() {
		this.controlAction = new QuestionUI_ControlAction(this);
		final int num = btnLables.length;
		jButtons = new JButton[num];

		for (int i = 0; i < num; i++) {
			jButtons[i] = new JButton(btnLables[i]);
			add(jButtons[i]);

			if (i > 1) {
				jButtons[i].addActionListener(controlAction);
			}
		}

		btnShowTS = new JButton("Show script");
		btnShowTS.addActionListener(controlAction);
		add(btnShowTS);

		PlayerAction playerAction = new PlayerAction(this);
		jButtons[0].addActionListener(playerAction);
		jButtons[1].addActionListener(playerAction);
		ImageIcon icon = new ImageIcon(getClass()
				.getResource("/Icons/play.png"));
		jButtons[0].setIcon(icon);
		icon = new ImageIcon(getClass().getResource("/Icons/stop.png"));
		jButtons[1].setIcon(icon);
		icon = new ImageIcon(getClass().getResource("/Icons/answer.png"));
		jButtons[2].setIcon(icon);
	}

	public void showTapeScript(boolean b) {
		if (b) {
			// get tape script text
			String text = getTextSource().getPathFileTS(index);
			if (text == null) {
				text = "There is no available tape script!";
			}
			jTextArea.setText(text);
		}

		jScrollPane.setVisible(b);
		jScrollPane.setEnabled(b);

		for (int i = 0; i < jLabels.length; i++) {
			jLabels[i].setEnabled(!b);
			jLabels[i].setVisible(!b);
			for (JCheckBox jCheckBox : jCheckBoxes[i]) {
				jCheckBox.setEnabled(!b);
				jCheckBox.setVisible(!b);
			}
		}
	}

	public boolean isAutoAddHead() {
		return autoAddHead;
	}

	public void setAutoAddHead(boolean autoAddHead) {
		this.autoAddHead = autoAddHead;
	}

	public TextSource getTextSource() {
		return textSource;
	}

	public JLabel[] getjLabels() {
		return jLabels;
	}

	public JCheckBox[][] getjCheckBoxes() {
		return jCheckBoxes;
	}

	public String[] getBtnLables() {
		return btnLables;
	}

	public String[] getBtnLables2() {
		return btnLables2;
	}

	public JButton[] getjButtons() {
		return jButtons;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public boolean isShowAnsewr() {
		return showAnsewr;
	}

	public void setShowAnsewr(boolean showAnsewr) {
		this.showAnsewr = showAnsewr;
	}

	public int getMaxIndex() {
		return maxIndex;
	}

	public JButton getBtnShowTS() {
		return btnShowTS;
	}

	public JScrollPane getjScrollPane() {
		return jScrollPane;
	}

	public MediaPlayer getMediaPlayer() {
		return mediaPlayer;
	}
}
